/* 
 * $Id: TaskPaneDemo.java 1218 2009-11-25 11:47:28Z kleopatra $ 
 * 
 * Copyright 2009 Sun Microsystems, Inc., 4150 Network Circle, 
 * Santa Clara, California 95054, U.S.A. All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version. 
 *  
 * This library is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 * Lesser General Public License for more details. 
 *  
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library; if not, write to the Free Software 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 */
//package org.jdesktop.swingx.demos.taskpane; 
package com.laozhao.msas.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.html.HTMLDocument;

import org.jdesktop.swingx.JXCollapsiblePane;
import org.jdesktop.swingx.JXMultiSplitPane;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.JXTaskPane;
import org.jdesktop.swingx.JXTaskPaneContainer;
import org.jdesktop.swingx.MultiSplitLayout;
import org.netbeans.swing.tabcontrol.DefaultTabDataModel;
import org.netbeans.swing.tabcontrol.TabData;
import org.netbeans.swing.tabcontrol.TabDataModel;
import org.netbeans.swing.tabcontrol.TabbedContainer;

//import com.sun.swingset3.DemoProperties; 

/**
 * A demo for the {@code JXTaskPane}.
 * 
 * @author Karl George Schaefer
 * @author l2fprod (original JXTaskPaneDemoPanel)
 */
// @DemoProperties(
// value = "JXTaskPane Demo",
// category = "Containers",
// description =
// "Demonstrates JXTaskPane, a container for tasks and other arbitrary components.",
// sourceFiles = {
// "org/jdesktop/swingx/demos/taskpane/TaskPaneDemo.java",
// "org/jdesktop/swingx/demos/taskpane/resources/TaskPaneDemo.properties",
// "org/jdesktop/swingx/demos/taskpane/resources/images/TaskPaneDemo.png",
// "org/jdesktop/swingx/demos/taskpane/resources/images/tasks-email.png",
// "org/jdesktop/swingx/demos/taskpane/resources/images/tasks-internet.png",
// "org/jdesktop/swingx/demos/taskpane/resources/images/tasks-question.png",
// "org/jdesktop/swingx/demos/taskpane/resources/images/tasks-recycle.png",
// "org/jdesktop/swingx/demos/taskpane/resources/images/tasks-writedoc.png"
// }
// )
@SuppressWarnings("serial")
public class TaskPaneDemo extends JPanel {
	private JXTaskPane systemGroup;
	private JXTaskPane officeGroup;
	private JXTaskPane seeAlsoGroup;
	private JXTaskPane detailsGroup;

//	private TabDataModel tabbedDataModel = new DefaultTabDataModel();
//	private TabbedContainer tabbedContainer = new TabbedContainer(tabbedDataModel);
	private TabDataModel tabbedDataModel;
	private TabbedContainer tabbedContainer;

	/**
	 * main method allows us to run as a standalone demo.
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				// JFrame frame = new JFrame(TaskPaneDemo.class.getAnnotation(
				// DemoProperties.class).value());
				JFrame frame = new JFrame("title 1");

//				JFrame frame2 = new JFrame("title 2");
//				frame2.setPreferredSize(new Dimension(800, 600));
//				frame2.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.getContentPane().add(new TaskPaneDemo());
				frame.setPreferredSize(new Dimension(800, 600));
				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
//				FileChooseAction action = new FileChooseAction("");
//				JXTaskPane taskPane = new JXTaskPane();
//				JLabel label = new JLabel();
//				
//				taskPane.add
			}
		});
	}

	public TaskPaneDemo() {
		super(new BorderLayout());

		createTaskPaneDemo();

		// Application.getInstance().getContext().getResourceMap(getClass())
		// .injectComponents(this);
		// ApplicationActionMap map = ApplicationActionMap.getInstance();

		bind();
	}

	private void createTaskPaneDemo() {
		JXTaskPaneContainer tpc = new JXTaskPaneContainer();
		
		// "System" GROUP
		systemGroup = new JXTaskPane();
		systemGroup.setTitle("systemGroup");
		systemGroup.setName("systemGroup");
		systemGroup.setSpecial(true); 
		tpc.add(systemGroup);

		// "Office" GROUP
		officeGroup = new JXTaskPane();
		officeGroup.setName("officeGroup");
//		officeGroup.setSpecial(true); 
		tpc.add(officeGroup);

		// "SEE ALSO" GROUP and ACTIONS
		seeAlsoGroup = new JXTaskPane();
		seeAlsoGroup.setName("seeAlsoGroup");
		seeAlsoGroup.setSpecial(false); 
//		tpc.add(seeAlsoGroup);

		// "Details" GROUP
		detailsGroup = new JXTaskPane();
		detailsGroup.setName("detailsGroup");

		// TODO better injection for editor area
		JEditorPane area = new JEditorPane("text/html", "<html>");
		area.setName("detailsArea");

		area.setFont(UIManager.getFont("Label.font"));

		Font defaultFont = UIManager.getFont("Button.font");

		String stylesheet = "body { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0; font-family: "
				+ defaultFont.getName()
				+ "; font-size: "
				+ defaultFont.getSize()
				+ "pt;  }"
				+ "a, p, li { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0; font-family: "
				+ defaultFont.getName()
				+ "; font-size: "
				+ defaultFont.getSize() + "pt;  }";
		if (area.getDocument() instanceof HTMLDocument) {
			HTMLDocument doc = (HTMLDocument) area.getDocument();
			try {
				doc.getStyleSheet().loadRules(
						new java.io.StringReader(stylesheet), null);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}

		detailsGroup.add(area);
		detailsGroup.add(seeAlsoGroup);
		tpc.add(detailsGroup);
		add(new JScrollPane(tpc), BorderLayout.WEST);

//		JTabbedPane centerPane = new JTabbedPane();

//		centerPane.addTab("title1", null, testColl(), "JTabbedPane");
//		centerPane.addTab("title2", null, testColl2(), "JTabbedPane");
//		centerPane.addTab("title3", null, new JButton("title3"), "JTabbedPane");
		
		TabData tabData1 = new TabData( testColl(),null,"title1", "JTabbedPane1");
		TabData tabData2 = new TabData( testColl2(),null,"title2", "JTabbedPane2");
		JButton btn3 = new JButton("title3");
		final JButton btn4 = new JButton("test action");
		final TabData tabX = new TabData( btn4 ,null,"action", "JTabbedPaneX");
		btn3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
//				TabData tab = new TabData( btn4 ,null,"action", "JTabbedPaneX");
				tabbedDataModel.addTab(tabbedDataModel.getTabs().size(), tabX);
			}
		});
		TabData tabData3 = new TabData( btn3 ,null,"title3", "JTabbedPane3");
		
//		tabbedDataModel.addTab(0,new TabData( testColl(),null,"title1", "JTabbedPane1"));
//		tabbedDataModel.addTab(1,new TabData( testColl2(),null,"title1", "JTabbedPane2"));
//		tabbedDataModel.addTab(2,new TabData( new JButton("title3"),null,"title1", "JTabbedPane3"));

		String[] data = { "one", "two", "three", "four" };
		JList list = new JList(data);
		// list.add(new JMenuItem(("exploreInternet")));
		// // list.add(new JMenuItem(map.get("exploreInternet")));
		// list.add(new JMenuItem(("help")));
		// list.add(new JMenuItem(("menu")));
		JScrollPane scrollPane = new JScrollPane(list);
//		centerPane.addTab("title4", null, scrollPane, "JTabbedPane");
//		centerPane.addTab("title5", null, testTools(), "JTabbedPane");
//		tabbedDataModel.addTab(3,new TabData( scrollPane,null,"title1", "JTabbedPane4"));
//		tabbedDataModel.addTab(4,new TabData( testTools(),null,"title1", "JTabbedPane5"));

		TabData tabData4 = new TabData( scrollPane,null,"title4", "JTabbedPane4");
		TabData tabData5 = new TabData( testTools(),null,"title5", "JTabbedPane5");
		tabbedDataModel = new DefaultTabDataModel(new TabData[] { tabData1,tabData2,tabData3,tabData4,tabData5});
		
//		add(centerPane, BorderLayout.CENTER);
//		tabbedContainer.getModel();
		  tabbedContainer = new TabbedContainer(tabbedDataModel,
				    TabbedContainer.TYPE_EDITOR);
		 
//		tabbedContainer.add(centerPane);
//		  tabbedContainer.s
		add(tabbedContainer ,BorderLayout.CENTER);
		JPanel messagePanel = new JPanel(new FlowLayout());
		// messagePanel.setBackground(Color.darkGray);
		JLabel messagelabel = new JLabel(
				"admin sysountjkjkjkjkjkjkkkkkkkkkkkkkkkkkkkkkkkkk");
//		messagelabel.setHorizontalAlignment(SwingConstants.LEFT);
		// messagelabel.set
		messagePanel.add(messagelabel);
//		JToolBar toolBar = new JToolBar();
//		toolBar.add(testColl2());
		add(messagePanel, BorderLayout.NORTH);
	}

	private JPanel testTools() {

		JPanel mainPanel = new JPanel(new BorderLayout());
		JPanel topMessagePanel = new JPanel();
		topMessagePanel.add(new JLabel("this a message panel"),
				BorderLayout.WEST);
		JButton xButton = new JButton("-X-");
		topMessagePanel.add(xButton, BorderLayout.EAST);
		 mainPanel.add(topMessagePanel,BorderLayout.NORTH);

		String[] columnNames = { "no", "name", "des" };
		String[][] tabledata = { { "1", "joy", "joy home" },
				{ "2", "tom", "tom cn" }, { "3", "eye", "java eye" }, };
		DefaultTableModel model = new DefaultTableModel(tabledata, columnNames);
		JXTable table = new JXTable(model);
		JScrollPane scrollTablePane = new JScrollPane(table);

		JPanel tmpPanel = new JPanel();
//		tmpPanel.add(topMessagePanel, BorderLayout.NORTH);
//		tmpPanel.add(scrollTablePane, BorderLayout.CENTER);
//		mainPanel.add(tmpPanel, BorderLayout.NORTH);
		mainPanel.add(scrollTablePane, BorderLayout.CENTER);

		JXCollapsiblePane collPanel = new JXCollapsiblePane();
//		collPanel.getContentPane().setLayout(new GridLayout(3, 2));
//		for (int i = 0; i < 3; i++) {
//			for (int j = 0; j < 2; j++) {
//				if (j % 2 == 0) {
//					collPanel.add(new JLabel("demo" + i + j));
//				} else {
//					collPanel.add(new JTextField("demo" + i + j));
//				}
//			}
//		}

		JToolBar toolBar = new JToolBar(SwingConstants.VERTICAL);
//		toolBar.add(testColl2());
		JXMultiSplitPane msp = new JXMultiSplitPane();

		String layoutDef = "(COLUMN (ROW weight=0.8 (COLUMN weight=0.25 (LEAF name=left.top weight=0.5) (LEAF name=left.middle weight=0.5))"
								+ " (LEAF name=editor weight=0.75)) (LEAF name=bottom weight=0.2))";
//		String layoutDef = "(COLUMN(LEAF name=editor weight=0.7) (LEAF name=bottom weight=0.3))";
		
		MultiSplitLayout.Node modelRoot = MultiSplitLayout
				.parseModel(layoutDef);
		msp.getMultiSplitLayout().setModel(modelRoot);

		msp.add(new JButton("Left Top"), "left.top");
		msp.add(new JButton("Left Middle"), "left.middle");
		msp.add(new JButton("Editor"), "editor");
		msp.add(new JButton("Bottom"), "bottom");

		// ADDING A BORDER TO THE MULTISPLITPANE CAUSES ALL SORTS OF ISSUES
		msp.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));

//		toolBar.add(msp, BorderLayout.CENTER);
		collPanel.getContentPane().setLayout(new BorderLayout(3, 2));
//		collPanel.add(toolBar, BorderLayout.CENTER);
		
		collPanel.add(msp,BorderLayout.CENTER);
		toolBar.add(collPanel,BorderLayout.CENTER);
		
//		mainPanel.add(collPanel, BorderLayout.SOUTH);
		mainPanel.add(toolBar, BorderLayout.SOUTH);
		collPanel.setCollapsed(true);
		xButton.setAction(collPanel.getActionMap().get(
				JXCollapsiblePane.TOGGLE_ACTION));
		
		
		return mainPanel;
		
		
//		return toolBar;
	}
	
	private JPanel testColl2() {

		JXMultiSplitPane msp = new JXMultiSplitPane();

//		String layoutDef = "(COLUMN (ROW weight=0.8 (COLUMN weight=0.25 (LEAF name=left.top weight=0.5) (LEAF name=left.middle weight=0.5))"
//								+ " (LEAF name=editor weight=0.75)) (LEAF name=bottom weight=0.2))";
		String layoutDef = "(COLUMN(LEAF name=editor weight=0.7) (LEAF name=bottom weight=0.3))";
		
		MultiSplitLayout.Node modelRoot = MultiSplitLayout
				.parseModel(layoutDef);
		msp.getMultiSplitLayout().setModel(modelRoot);
//
//		msp.add(new JButton("Left Top"), "left.top");
//		msp.add(new JButton("Left Middle"), "left.middle");
//		msp.add(new JButton("Editor"), "editor");
//		msp.add(new JButton("Bottom"), "bottom");

		// ADDING A BORDER TO THE MULTISPLITPANE CAUSES ALL SORTS OF ISSUES
		msp.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));

//		add(msp, BorderLayout.CENTER);

//		JPanel mainPanel = new JPanel(new BorderLayout());
		JPanel topMessagePanel = new JPanel();
		topMessagePanel.add(new JLabel("this a message panel"),
				BorderLayout.WEST);
		JButton xButton = new JButton("-X-");
		topMessagePanel.add(xButton, BorderLayout.EAST);
		// mainPanel.add(topMessagePanel,BorderLayout.NORTH);

		String[] columnNames = { "no", "name", "des" };
		String[][] tabledata = { { "1", "joy", "joy home" },
				{ "2", "tom", "tom cn" }, { "3", "eye", "java eye" }, };
		DefaultTableModel model = new DefaultTableModel(tabledata, columnNames);
		JXTable table = new JXTable(model);
		JScrollPane scrollTablePane = new JScrollPane(table);

		JPanel tmpPanel = new JPanel(new BorderLayout());
		tmpPanel.add(topMessagePanel, BorderLayout.NORTH);
		tmpPanel.add(scrollTablePane, BorderLayout.CENTER);
		msp.add(tmpPanel, "editor");

		JXCollapsiblePane collPanel = new JXCollapsiblePane();
		collPanel.getContentPane().setLayout(new GridLayout(3, 2));
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 2; j++) {
				if (j % 2 == 0) {
					collPanel.add(new JLabel("demo" + i + j));
				} else {
					collPanel.add(new JTextField("demo" + i + j));
				}
			}
		}
//		mainPanel.add(collPanel, BorderLayout.CENTER);
		msp.add(collPanel, "bottom");
		collPanel.setCollapsed(true);
		xButton.setAction(collPanel.getActionMap().get(
				JXCollapsiblePane.TOGGLE_ACTION));
		return msp;
	}

	private JPanel testColl() {

		JPanel mainPanel = new JPanel(new BorderLayout());
		JPanel topMessagePanel = new JPanel();
		topMessagePanel.add(new JLabel("this a message panel"),
				BorderLayout.WEST);
		JButton xButton = new JButton("-X-");
		topMessagePanel.add(xButton, BorderLayout.EAST);
		 mainPanel.add(topMessagePanel,BorderLayout.NORTH);

		String[] columnNames = { "no", "name", "des" };
		String[][] tabledata = { { "1", "joy", "joy home" },
				{ "2", "tom", "tom cn" }, { "3", "eye", "java eye" }, };
		DefaultTableModel model = new DefaultTableModel(tabledata, columnNames);
		JXTable table = new JXTable(model);
		JScrollPane scrollTablePane = new JScrollPane(table);

		JPanel tmpPanel = new JPanel();
//		tmpPanel.add(topMessagePanel, BorderLayout.NORTH);
//		tmpPanel.add(scrollTablePane, BorderLayout.CENTER);
//		mainPanel.add(tmpPanel, BorderLayout.NORTH);
		mainPanel.add(scrollTablePane, BorderLayout.CENTER);

		JXCollapsiblePane collPanel = new JXCollapsiblePane();
		collPanel.getContentPane().setLayout(new GridLayout(3, 2));
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 2; j++) {
				if (j % 2 == 0) {
					collPanel.add(new JLabel("demo" + i + j));
				} else {
					collPanel.add(new JTextField("demo" + i + j));
				}
			}
		}
		mainPanel.add(collPanel, BorderLayout.SOUTH);
		collPanel.setCollapsed(true);
		xButton.setAction(collPanel.getActionMap().get(
				JXCollapsiblePane.TOGGLE_ACTION));
		return mainPanel;
	}

	private void bind() {
		ApplicationActionMap map = ApplicationActionMap.getInstance();

		systemGroup.add(map.get("email"));
		systemGroup.add(map.get("delete"));
//		systemGroup.add(new LabelAction());
		

		JButton xButton = new JButton(new LabelAction(" name1 "));
		xButton.setText("--------");
		
		xButton.setOpaque(true);
		xButton.addActionListener(new LabelAction(" + name2 +"));
		xButton.setAction(new LabelAction(" + name3 +"));
		// xButton.setBorder(emptyBorder);
		// xButton.setContentAreaFilled(false);
		xButton.setFocusPainted(false);
		xButton.setRolloverEnabled(true);

		officeGroup.add(map.get("write"));
		officeGroup.add(new JButton("hello button"));
		officeGroup.add(new JButton(map.get("test")));
		officeGroup.add(xButton);
		// officeGroup.add(new AbstractButton());

		seeAlsoGroup.add(new JMenuItem(map.get("exploreInternet")));
		seeAlsoGroup.add(new JMenuItem(map.get("help")));
		seeAlsoGroup.add(new JMenuItem(map.get("menu")));

		FileChooseControl control = new FileChooseControl();
//		officeGroup.add;
		// String[] data = {"one", "two", "three", "four"};
		// JList list = new JList(data);
		// JList list = new JList();
		// list.add(new JMenuItem(map.get("exploreInternet")));
		// // list.add(new JMenuItem(map.get("exploreInternet")));
		// list.add(new JMenuItem(map.get("help")));
		// list.add(new JMenuItem(map.get("menu")));
		// seeAlsoGroup.add(list);
	}

	// @Action
	public void email() {
	}

	// @Action
	public void delete() {
	}

	// @Action
	public void write() {
	}

	// @Action
	public void exploreInternet() {
	}

	// @Action
	public void help() {
	}

	// new AbstractButton(){
	//		
	// };

	// class ApplicationActionMap{
	// static ApplicationActionMap getInstance(){
	// return new ApplicationActionMap();
	// }
	//		
	// };
	// public static void main(String[] args) {
	//
	// JXFrame frame = new JXFrame();
	//
	// // a container to put all JXTaskPane together
	// JXTaskPaneContainer taskPaneContainer = new JXTaskPaneContainer();
	//
	// // create a first taskPane with common actions
	// JXTaskPane actionPane = new JXTaskPane();
	// actionPane.setTitle("Files and Folders");
	// actionPane.setSpecial(true);
	//
	// // actions can be added, a hyperlink will be created
	// Action renameSelectedFile = createRenameFileAction();
	// actionPane.add(renameSelectedFile);
	// actionPane.add(createDeleteFileAction());
	//
	// // add this taskPane to the taskPaneContainer
	// taskPaneContainer.add(actionPane);
	//
	// // create another taskPane, it will show details of the selected file
	// JXTaskPane details = new JXTaskPane();
	// details.setTitle("Details");
	//
	// // add standard components to the details taskPane
	// JLabel searchLabel = new JLabel("Search:");
	// JTextField searchField = new JTextField("");
	// details.add(searchLabel);
	// details.add(searchField);
	//
	// taskPaneContainer.add(details);
	//
	// // put the action list on the left
	// frame.add(taskPaneContainer, BorderLayout.EAST);
	//
	// // and a file browser in the middle
	// frame.add(fileBrowser, BorderLayout.CENTER);
	//
	// frame.pack();
	// frame.setVisible(true);
	// }
	//
	// private static Action createRenameFileAction() {
	// // TODO Auto-generated method stub
	// return null;
	// }

}